#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QWizard>
#include <QLabel>
#include <QVBoxLayout>
#include <QButtonGroup>
#include <QRadioButton>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    _progdilg = new QProgressDialog(tr("正在复制"), tr("取消复制"), 0, 5000, this);
    _progdilg->setWindowTitle(tr("进度对话框"));
    _progdilg->setWindowModality(Qt::ApplicationModal);

    _timer = new QTimer(this);


    connect(_timer, &QTimer::timeout, this, &MainWindow::updateTimer);
    // 放在这里是最合适的，放在前面会导致提前计时
    // 每2ms触发一次timeout事件
    _timer->start(2);
    connect(_progdilg, &QProgressDialog::canceled, this, &MainWindow::cancelTimer);
}

void MainWindow::updateTimer()
{
    _counter++;
    if (_counter > 5000)
    {
        _timer->stop();
        delete _timer;
        _timer = nullptr;
        delete _progdilg;
        _progdilg = nullptr;
        _counter = 0;
        return;
    }
    _progdilg->setValue(_counter);
    qDebug() << "复制开始";
}

void MainWindow::cancelTimer()
{
    _timer->stop();
    delete _timer;
    _timer = nullptr;
    delete _progdilg;
    _progdilg = nullptr;
    _counter = 0;
    qDebug() << "复制取消";
}


// 1. 创建一个向导对话框
// 2. 创建一个向导页
// 3. 创建一个标签
// 4. 创建一个layout对象
// 4. 给每个层级设置自己的属性
// 5. 后续的程序设计一个butttongroup
void MainWindow::on_pushButton_2_clicked()
{
    QWizard wizard(this);
    wizard.setWindowTitle(tr("程序设计语言选择"));
    wizard.setWindowModality(Qt::ApplicationModal);
    QWizardPage *page1 = new QWizardPage();
    page1->setTitle("程序设计语言选择引导");
    QLabel *label1 = new QLabel();
    label1->setText(tr("该程序帮助您选择合适的程序设计语言"));
    QVBoxLayout *layout1 = new QVBoxLayout();
    layout1->addWidget(label1);
    page1->setLayout(layout1);
    wizard.addPage(page1);

    QWizardPage *page2 = new QWizardPage();
    page2->setTitle(tr("选择程序设计语言"));


    QButtonGroup *group = new QButtonGroup(page2);
    QRadioButton *btn1 = new QRadioButton();
    btn1->setText("Assembly");
    group->addButton(btn1);

    QRadioButton *btn2 = new QRadioButton();
    btn2->setText("Bash");
    group->addButton(btn2);


    QRadioButton *btn3 = new QRadioButton();
    btn3->setText("C/C++");
    group->addButton(btn3);


    QRadioButton *btn4 = new QRadioButton();
    btn4->setText("Dart");
    group->addButton(btn4);


    QRadioButton *btn5 = new QRadioButton();
    btn5->setText("Elm");
    group->addButton(btn5);

    QVBoxLayout * layout2 = new QVBoxLayout();
    for (int i = 0; i < group->buttons().size(); i++)
    {
        layout2->addWidget(group->buttons()[i]);
    }
    page2->setLayout(layout2);
    wizard.addPage(page2);
    wizard.show();
    wizard.exec();

}

